package com.cbgolf.oa.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.cbgolf.oa.util.ViewUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public abstract class MyBaseAdapter<T> extends BaseAdapter {

  protected List<T> list;
  protected Context context;
  protected int itemId;
  protected LayoutInflater flater;
  protected View currView;
  public int lastAddCount = 0;
  private Map<Integer, SoftReference<View>> viewCaches = null;
  DisplayImageOptions options = new DisplayImageOptions.Builder()
//            .showImageOnLoading(R.drawable.banner_usercenter) // 设置图片下载期间显示的图片
      .showImageForEmptyUri(new ColorDrawable(0xf2f2f2)) // 设置图片Uri为空或是错误的时候显示的图片
      .showImageOnFail(new ColorDrawable(0xf2f2f2)) // 设置图片加载或解码过程中发生错误显示的图片
      .cacheInMemory(true) // default  设置下载的图片是否缓存在内存中
      .cacheOnDisk(true) // default  设置下载的图片是否缓存在SD卡中
      .imageScaleType(ImageScaleType.EXACTLY) // default 设置图片以如何的编码方式显示
      .bitmapConfig(Bitmap.Config.RGB_565) // default 设置图片的解码类型
//                .displayer(new SimpleBitmapDisplayer()) // default  还可以设置圆角图片new RoundedBitmapDisplayer(20)
//                .displayer(new FadeInBitmapDisplayer(100))// 图片加载好后渐入的动画时间
      .build();
  ImageLoader imageLoader = ImageLoader.getInstance();


  public MyBaseAdapter(Context context, T[] arrays) {
    super();
    this.list = new ArrayList<T>();
    for (T t : arrays) {
      this.list.add(t);
    }
    this.context = context;
  }

  public MyBaseAdapter(Context context, List<T> list) {
    super();
    this.list = list;
    if (null == this.list) {
      this.list = new ArrayList<T>();
    }
    this.context = context;
  }

  public MyBaseAdapter(int itemId, Context context, List<T> list) {
    super();
    this.list = list;
    if (null == this.list) {
      this.list = new ArrayList<T>();
    }
    this.itemId = itemId;
    this.context = context;
    flater = LayoutInflater.from(context);
    currView = flater.inflate(itemId, null);
  }

  public MyBaseAdapter(int itemId, Context context, T[] arrays) {
    super();
    this.list = new ArrayList<T>();
    for (T t : arrays) {
      this.list.add(t);
    }
    this.itemId = itemId;
    this.context = context;
    flater = LayoutInflater.from(context);
    currView = flater.inflate(itemId, null);
  }

  public void clear() {
    this.list.clear();
    System.gc();
  }

  public void remove(int position) {
    if (null != list && 0 < list.size()) {
      list.remove(position);
    }
  }

  public void remove(T t) {
    if (null != list && 0 < list.size()) {
      list.remove(t);
    }
  }

  public void add(T t) {
    this.lastAddCount = 1;
    this.list.add(t);
  }

  public void add(List<T> list) {
    this.lastAddCount = list.size();
    this.list.addAll(list);
  }

  public void add(T[] arrays) {
    for (T t : arrays) {
      this.list.add(t);
    }
  }

  public void setList(List<T> list) {
    this.list = list;
  }

  public void updateUI() {
    this.notifyDataSetChanged();
  }

  @Override
  public int getCount() {
    return list.size();
  }

  @Override
  public T getItem(int position) {
    return list.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  public int getLastAddCount() {
    return lastAddCount;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    synchronized (this) {
      if (0 == list.size()) {
        return convertView;
      }
    }
    T t = list.get(position);
    if (null == convertView) {
      convertView = currView = flater.inflate(itemId, null);
      currView.setTag(viewCaches = new HashMap<Integer, SoftReference<View>>());
    } else {
      currView = convertView;
      viewCaches = (HashMap<Integer, SoftReference<View>>) currView
          .getTag();
    }
    return getView(position, convertView, parent, t);
  }

  protected ImageView setImage(int viewId, String url) {
    ImageView view = (ImageView) getCacheView(viewId);
    imageLoader.displayImage(url, view, options);
//        BitmapUtils.loadBitmap(url, view);
    return view;
  }

  protected ImageView setImageRes(int viewId, int resId) {
    ImageView view = (ImageView) getCacheView(viewId);
    view.setImageResource(resId);
    return view;
  }

  protected ImageView setImage(int viewId, String url, int w, int h) {
    ImageView view = (ImageView) getCacheView(viewId);
//    BitmapUtils.loadBitmap(url, view, w, h);
        imageLoader.displayImage(url,view,options);
    return view;
  }

  protected TextView setText(int viewId, CharSequence text) {
    TextView view = (TextView) getCacheView(viewId);
    ViewUtils.setText(view, text);
    return view;
  }

  protected void setVisible(int viewId, boolean visible) {
    ViewUtils.setVisible(getCacheView(viewId), visible);
  }

  protected void setINVisible(int viewId, boolean InVisible) {
    ViewUtils.setInVisible(getCacheView(viewId), InVisible);
  }

  protected void setBackgroundColor(int viewId, int color) {
    getCacheView(viewId).setBackgroundColor(color);
  }

  @SuppressLint("NewApi")
  protected void setBackground(int viewId, int res) {
    getCacheView(viewId).setBackground(ContextCompat.getDrawable(context, res));
  }

  protected void setTextColor(int viewId, int color) {
    TextView view = (TextView) getCacheView(viewId);
    if (null != view) {
      view.setTextColor(color);
    }
  }

  protected void setOnClick(int id, View.OnClickListener listener) {
    getCacheView(id).setOnClickListener(listener);
  }


  protected void setBackgroupdRes(int id, int res) {
    View view = getCacheView(id);
    if (null != view) {
      view.setBackgroundResource(res);
    }
  }

  protected void setBackgroupdColor(int id, int color) {
    View view = getCacheView(id);
    if (null != view) {
      view.setBackgroundColor(color);
    }
  }

  protected View getCacheView(int viewId) {
    SoftReference<View> cacheView = viewCaches.get(viewId);
    View view = null;
    if (null == cacheView || null == cacheView.get()) {
      cacheView = new SoftReference<View>(currView.findViewById(viewId));
    }
    view = cacheView.get();
    return view;
  }

  public abstract View getView(int position, View convertView,
      ViewGroup parent, T t);
}
